import api from './api'
// 获取星期
const getWeekColumns = (num = 4) => {
  const cnum = ['一', '二', '三', '四', '五', '六', '日']
  const columns = []
  for (let idx = 0; idx < num; idx++) {
    const column = {
      title: `第${cnum[idx]}周`, align: 'center', children: []
    }
    let colFirstIndex = cnum.length * idx
    for (let i = 0; i < cnum.length; i++) {
      const colIndex = colFirstIndex + i
      column.children.push(
        {
          title: `星期${cnum[i]}`, align: 'center', width: 110, children: [
            {
              title: '星期一', align: 'center', width: 110, slots: {
                header: ({ $table }) => {
                  const data = $table.getData()
                  const row = data.find(d => d.date)
                  return row?.date[colIndex]?.date
                },
                default: ({ row }) => row.date[colIndex]?.total_price || '-'
              }
            }]
        },
      )
    }
    columns.push(column)
  }
  return columns
}
export default {
  1: (setOptions, handleRow, data) => ({
    rowConfig: { height: 'auto' },
    scrollY: { enabled: false },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 1,
        dateRange: { type: 'except_payment_time', value: [], range: true },
        ...data
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '货款类型', field: 'cash_type_name', width: 100 },
      { title: '经办人', field: 'purchase_user_name', width: 120, line: 2 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      { title: '供应商id', field: 'supplier_code', width: 80 },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      {
        title: '需求部门', field: 'department_name', minWidth: 120, slots: {
          default: ({ row }) => <VText value={row.department_name} line={2} />
        }
      },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '货款总额', field: 'total_price', width: 100 },
      { title: '修改后货款总额', field: 'edit_total_price', width: 100 },
      { title: '下周付款原币金额', field: 'week_payment_price', width: 90 },
      { title: '汇率', field: 'rate', width: 80 },
      { title: '下周付款人民币金额', field: 'week_payment_cny', width: 90 },
      { title: '需求付款日期', field: 'except_payment_time', width: 110 },
      { title: '实付金额(CNY)', field: 'actual_payment_price', width: 110 },
      { title: '差异', field: 'diff_payment_price', width: 110 },
      {
        title: '付款主体', field: 'company_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.company_name} line={2} />
        }
      },
      {
        title: '采购备注', field: 'remark', width: 120, slots: {
          default: ({ row }) => <VText value={row.remark} line={2} />
        }
      },
      { title: '是否确认', field: 'status_name', width: 100 }
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.historyList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
  2: (setOptions, handleRow, data) => ({
    border: true,
    rowConfig: { height: 'auto' },
    scrollY: { enabled: false },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 2,
        dateRange: { type: 'except_payment_time', value: [], range: true },
        ...data
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '采购人员', field: 'purchase_user_name', width: 100 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      { title: '供应商id', field: 'supplier_code', width: 80 },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      { title: '货款总额', field: 'total_price', width: 80 },
      { title: '公司主体', field: 'company_name', width: 120 },
      { title: '部门', field: 'department_name', width: 80 },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '汇率', field: 'rate', width: 80 },
      ...getWeekColumns(),
      { title: '汇总', field: 'total_price', width: 80 },
      { title: '备注', field: 'remark', width: 100 },
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.historyFourList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
  3: (setOptions, handleRow) => ({
    rowConfig: { height: 'auto' },
    scrollY: { enabled: false },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 3,
        dateRange: { type: 'except_payment_time', value: [], range: true },
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '货款类型', field: 'cash_type_name', width: 100 },
      { title: '经办人', field: 'purchase_user_name', width: 120, line: 2 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      { title: '供应商id', field: 'supplier_code', width: 80 },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      {
        title: '需求部门', field: 'department_name', minWidth: 120, slots: {
          default: ({ row }) => <VText value={row.department_name} line={2} />
        }
      },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '货款总额', field: 'total_price', width: 80 },
      { title: '修改后货款总额', field: 'edit_total_price', width: 100 },
      { title: '下周付款原币金额', field: 'week_payment_price', width: 90 },
      { title: '汇率', field: 'rate', width: 80 },
      { title: '下周付款人民币金额', field: 'week_payment_cny', width: 90 },
      { title: '需求付款日期', field: 'except_payment_time', width: 110 },
      {
        title: '付款主体', field: 'company_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.company_name} line={2} />
        }
      },
      {
        title: '采购备注', field: 'remark', width: 120, slots: {
          default: ({ row }) => <VText value={row.remark} line={2} />
        }
      },
      { title: '是否确认', field: 'status_name', width: 100 },
      {
        title: '操作', width: 110, fixed: 'right', slots: {
          default: ({ row, rowIndex }) => {
            // cash_type: [ { "label": "采购货款", "value": 1 }, { "label": "采购预估", "value": 2 }, { "label": "到期应付", "value": 3 } ]
            // status: [ { "label": "已确认", "value": 1 }, { "label": "未确认", "value": 2 } ]
            const {cash_type, status} = row
            return <div class='nm flex flex-wrap gap-1 items-start'>
            {status === 2 ? <VButton auth='/purchase/manage/plan/editPrice' link type='primary' onClick={() => handleRow('editPrice', row, rowIndex)}>修改货款总额</VButton> : ''}
            {cash_type === 2 && status === 2 ? <VButton auth='/purchase/manage/plan/edit' link type='primary' onClick={() => handleRow('edit', row, rowIndex)}>编辑</VButton> : ''}
            {status === 2 ? <VButton auth='/purchase/manage/plan/batch' link type='primary' onClick={() => handleRow('confirm', row, rowIndex)}>确认</VButton> : ''}
            {cash_type === 2 && status === 2 ? <VButton auth='/purchase/manage/plan/delete' link type='primary' onClick={() => handleRow('delete', row, rowIndex)}>删除</VButton> : ''}
            <el-button link type='primary' onClick={() => handleRow('log', row, rowIndex)}>日志</el-button>
          </div>
          }
        }
      },
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.nextWeekList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
  4: (setOptions) => ({
    border: true,
    scrollY: { enabled: false },
    rowConfig: { height: 'auto' },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 4,
        dateRange: { type: 'except_payment_time', value: [], range: true },
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '采购人员', field: 'purchase_user_name', width: 100 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      { title: '供应商id', field: 'supplier_code', width: 80 },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      { title: '货款总额', field: 'total_price', width: 80 },
      {
        title: '付款主体', field: 'company_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.company_name} line={2} />
        }
      },
      {
        title: '部门', field: 'department_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.department_name} line={2} />
        }
      },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '汇率', field: 'rate', width: 80 },
      ...getWeekColumns(),
      { title: '汇总', field: 'total_price', width: 80 },
      { title: '备注', field: 'remark', width: 100 },
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.fourWeekList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
  5: (setOptions, handleRow) => ({
    rowConfig: { height: 'auto' },
    scrollY: { enabled: false },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 5,
        dateRange: { type: 'except_payment_time', value: [], range: true },
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '货款类型', field: 'cash_type_name', width: 100 },
      { title: '经办人', field: 'purchase_user_name', width: 120, line: 2 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      {
        title: '需求部门', field: 'department_name', minWidth: 120, slots: {
          default: ({ row }) => <VText value={row.department_name} line={2} />
        }
      },
      { title: '货款到期日', field: 'settlement_time', width: 100 },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '货款总额', field: 'total_price', width: 100 },
      { title: '月度付款原币金额', field: 'week_payment_price', width: 90 },
      { title: '汇率', field: 'rate', width: 80 },
      { title: '月度付款人民币金额', field: 'week_payment_cny', width: 90 },
      { title: '期望付款日期', field: 'except_payment_time', width: 110 },
      {
        title: '付款主体', field: 'company_name', minWidth: 120, slots: {
          default: ({ row }) => <VText value={row.company_name} line={2} />
        }
      },
      { title: '是否确认', field: 'status_name', width: 100 },
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.monthList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
  6: (setOptions, handleRow) => ({
    rowConfig: { height: 'auto' },
    scrollY: { enabled: false },
    showHeaderOverflow: false,
    showOverflow: false,
    formConfig: {
      data: {
        type: 3,
        dateRange: { type: 'except_payment_time', value: [], range: true },
      }
    },
    columns: [
      {type: 'checkbox', fixed: 'left', width: 40},
      { title: '货款类型', field: 'cash_type_name', width: 100 },
      { title: '经办人', field: 'purchase_user_name', width: 120, line: 2 },
      {
        title: '供应商名称', field: 'supplier_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.supplier_name} line={2} />
        }
      },
      { title: '供应商id', field: 'supplier_code', width: 80 },
      {
        title: '结算方式', field: 'period_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.period_name} line={2} />
        }
      },
      { title: '货款所属期', field: 'month', width: 100 },
      {
        title: '需求部门', field: 'department_name', minWidth: 120, slots: {
          default: ({ row }) => <VText value={row.department_name} line={2} />
        }
      },
      { title: '币种', field: 'currency_name', width: 110 },
      { title: '货款总额', field: 'total_price', width: 80 },
      { title: '修改后货款总额', field: 'edit_total_price', width: 100 },
      { title: '下周付款原币金额', field: 'week_payment_price', width: 90 },
      { title: '汇率', field: 'rate', width: 80 },
      { title: '下周付款人民币金额', field: 'week_payment_cny', width: 90 },
      { title: '需求付款日期', field: 'except_payment_time', width: 110 },
      {
        title: '付款主体', field: 'company_name', width: 120, slots: {
          default: ({ row }) => <VText value={row.company_name} line={2} />
        }
      },
      {
        title: '采购备注', field: 'remark', width: 120, slots: {
          default: ({ row }) => <VText value={row.remark} line={2} />
        }
      },
      { title: '是否确认', field: 'status_name', width: 100 },
      {
        title: '操作', width: 110, fixed: 'right', slots: {
          default: ({ row, rowIndex }) => {
            // cash_type: [ { "label": "采购货款", "value": 1 }, { "label": "采购预估", "value": 2 }, { "label": "到期应付", "value": 3 } ]
            // status: [ { "label": "已确认", "value": 1 }, { "label": "未确认", "value": 2 } ]
            const {cash_type, status} = row
            return <div class='nm flex flex-wrap gap-1 items-start'>
            {status === 2 ? <VButton auth='/purchase/manage/plan/editPrice' link type='primary' onClick={() => handleRow('editPrice', row, rowIndex)}>修改货款总额</VButton> : ''}
            {cash_type === 2 && status === 2 ? <VButton auth='/purchase/manage/plan/edit' link type='primary' onClick={() => handleRow('edit', row, rowIndex)}>编辑</VButton> : ''}
            {status === 2 ? <VButton auth='/purchase/manage/plan/batch' link type='primary' onClick={() => handleRow('confirm', row, rowIndex)}>确认</VButton> : ''}
            {cash_type === 2 && status === 2 ? <VButton auth='/purchase/manage/plan/delete' link type='primary' onClick={() => handleRow('delete', row, rowIndex)}>删除</VButton> : ''}
            <el-button link type='primary' onClick={() => handleRow('log', row, rowIndex)}>日志</el-button>
          </div>
          }
        }
      },
    ],
    proxyConfig: {
      ajax: {
        query: ({ page, form }) => api.weekList({ ...form, ...page }).then(res => {
          const { list } = res.data
          setOptions(res.data)
          return { data: list, total: res.total }
        })
      }
    }
  }),
}